package com.cjy.controller;


import cn.hutool.core.util.ObjectUtil;
import com.cjy.pojo.FasdfsFile;
import com.cjy.pojo.SystemResult;
import com.cjy.service.FastdfsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Controller
@RequestMapping("file")
public class FileController {

    @Autowired
    private FastdfsService fastdfsService;

    @Value("${fastdfs_domaint}")
    private String fastdfsDomaint;

    @PostMapping("/upload")
    @ResponseBody
    public SystemResult upload(@RequestParam("file") MultipartFile file,
                               @RequestParam("imageTypeId") String imageTypeId,
                               @RequestParam("isPrivate") String isPrivate) {
        if (ObjectUtil.isNull(file) || ObjectUtil.isEmpty(imageTypeId) || ObjectUtil.isEmpty(isPrivate)){
            return SystemResult.err("上传文件失败");
        }
        return fastdfsService.uploadFile(file,imageTypeId,isPrivate);
    }

    @PostMapping("/upload2")
    @ResponseBody
    public SystemResult upload2(@RequestParam("file") MultipartFile file,
                               @RequestParam("imageTypeId") String imageTypeId,
                               @RequestParam("isPrivate") String isPrivate) {
        if (ObjectUtil.isNull(file) || ObjectUtil.isEmpty(imageTypeId) || ObjectUtil.isEmpty(isPrivate)){
            return SystemResult.err("上传文件失败");
        }
        return fastdfsService.uploadFile2(file,imageTypeId,isPrivate);
    }

    @GetMapping("/remove")
    @ResponseBody
    public SystemResult remove(@RequestParam("id") Long id) {
        return fastdfsService.deleteImage(id);
    }

    @GetMapping("/pictures")
    public String pictures(Model model) {
        List<FasdfsFile> fasdfsFileList = fastdfsService.findTop();
        model.addAttribute("pictures",fasdfsFileList);
        model.addAttribute("header",fastdfsDomaint);
        return "picture";
    }

}
